home *** CD-ROM | disk | FTP | other *** search
Text File | 1991-05-01 | 8.8 KB | 386 lines | [TEXT/MPS ] |
- {[a-,body+,h-,o=100,r+,rec+,t=4,u+,#+,j=20/57/1$,n-]}
-
- USES UList, UAssociation, UFailure, TextEdit, OSUtils, UMacAppUniversal, Editions, UMacAppUtilities, UPatch,
- Events, CursorCtl, Signal, PasLibIntf, IntEnv, ErrMgr, Packages, Script, Resources, Fonts;
-
- {--------------------------------------------------------------------------------------------------}
-
- {$S TInit}
-
- PROCEDURE InitUPascalTool;
-
- BEGIN
- gPascalTool := NIL;
- { Do Tool related initialization }
- InitGraf(@thePort);
- SetFScaleDisable(true); { per chapter in MPW guide on tools }
-
- InitCursorCtl(NIL);
- RotateCursor(0);
-
- InitErrMgr('', '', false);
-
- gProgName := ArgV^[0]^;
-
- {$IFC qDebug} { Enable pre and postcondition testing }
- gPreCondition := TRUE;
- gPostCondition := TRUE;
- {$ENDC}
-
- { -1 = $FFFFFFFF, the largest 32 bit address. Our routine StripLong uses a pre-stripped
- address gStrippedAddress to avoid the yucky MPW glue.
- (NOTE: need gStrippedAddress in DefineConfiguration.) }
- gStrippedAddress := StripAddress(Ptr( - 1));
-
- { Find out just what kind of environment we're dealing with here }
- DefineConfiguration(gConfiguration);
-
- { Init the stuff that MATextBox uses }
- gMATextBoxTE := NIL;
- gTEDefaultWordBreak := NIL;
-
- { Init all the primary colors }
- SetRGBColor(gRGBBlack, 0, 0, 0);
- SetRGBColor(gRGBWhite, $FFFF, $FFFF, $FFFF);
- SetRGBColor(gRGBRed, $FFFF, 0, 0);
- SetRGBColor(gRGBGreen, 0, $FFFF, 0);
- SetRGBColor(gRGBBlue, 0, 0, $FFFF);
-
- { setup the zeroed points and rects }
- SetPt(gZeroPt, 0, 0);
- SetRect(gZeroRect, 0, 0, 0, 0);
-
- {$IFC qDebug OR qInspector}
- gFieldToStrRtn := @StdFieldToString;
- gFieldToCountRtn := @StdFieldToCount;
- {$EndC}
-
- gBoolString[TRUE] := 'TRUE';
- gBoolString[FALSE] := 'FALSE';
- gDeadStripSuppression := FALSE;
- { The refnum where the application's resources should be found }
- gApplicationRefNum := CurResFile;
-
- {$IFC qDebug}
- gExperimenting := FALSE;
- gDebugPrinting := FALSE;
- gReportMenuChoices := FALSE;
- gIntenseDebugging := FALSE;
- gReportEvt := FALSE;
- {$ENDC}
-
- gToolBoxInitialized := TRUE;
-
- { Do Object related initialization }
- InitUObject;
-
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TRes}
-
- PROCEDURE Intr;
-
- BEGIN
- gPascalTool.fInterrupted := true; {we test this switch periodically}
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TRes}
-
- PROCEDURE TPascalTool.Stop(msg: Str255);
-
- BEGIN
- IF Length(msg) > 0 THEN
- BEGIN
- PLFlush(Output);
- WriteLn(Diagnostic);
- WriteLn(Diagnostic, msg);
- END;
-
- IF fInterrupted THEN
- IEexit( - 9);
- { don't worry about closing the files we opened. The Shell
- will do so if appropriate.}
- IEexit(Ord(fRetCode)); {exit, returning the appropriate status
- code}
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.SyntaxError(suffix: Str255);
-
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := fProgName;
- PLFlush(Output);
- WriteLn(Diagnostic, kErrorMarker, 'Bad Parameter: ', suffix);
- WriteLn(Diagnostic, kErrorMarker, aStr, '<invalid option>');
- Stop('');
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.DoShowUsage;
-
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := fProgName;
- WriteLn(Diagnostic, '# Usage: ', aStr, ' [-p]');
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- FUNCTION TPascalTool.GetNextArg: Str255;
-
- BEGIN
- fArgVIndex := fArgVIndex + 1;
- IF fArgVIndex > ArgC THEN
- Stop('Not enough arguments');
- GetNextArg := ArgV^[fArgVIndex]^;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.InstallKeyWords;
-
- BEGIN
- InstallKeyWord('P', kwP);
- InstallKeyWord('NoP', kwNoP);
- InstallKeyWord('T', kwT);
- InstallKeyWord('NoT', kwNoT);
- InstallKeyWord('Help', kwHelp);
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.InstallKeyWord(keyword: Str255;
- kw: Integer);
-
- VAR
- value: Str255;
-
- BEGIN
- UprStr255(keyword);
- value[0] := chr(2);
- value[1] := chr(BSR(Band(kw, $FF00), 8));
- value[2] := chr(Band(kw, $00FF));
- fKeyWordList.InsertEntry(keyword, value);
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- FUNCTION TPascalTool.LookupKeyword(keyword: Str255;
- VAR kw: Integer): BOOLEAN;
-
- VAR
- value: Str255;
-
- BEGIN
- UprStr255(keyword);
- IF fKeyWordList.ValueAt(keyword, value) THEN
- BEGIN
- LookupKeyword := true;
- kw := BOR(BSL(ord4(value[1]), 8), Ord(value[2]));
- END
- ELSE
- LookupKeyword := false;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.ProcessArg(arg: Str255);
-
- VAR
- akw: Integer;
-
- BEGIN
- IF arg[1] <> '-' THEN
- DoProcessFileArg(arg)
- ELSE
- BEGIN
- IF LookupKeyword(copy(arg, 2, Length(arg) - 1), akw) THEN
- DoProcessOptionArg(akw)
- ELSE
- SyntaxError(Concat(ArgV^[fArgVIndex]^, ' <invalid option>'));
- END;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.DoProcessFileArg(arg: Str255);
-
- BEGIN
- SyntaxError(Concat(ArgV^[fArgVIndex]^, ' <invalid option>'));
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.DoProcessOptionArg(kw: Integer);
-
- BEGIN
- CASE kw OF
- kwP:
- fProgress := true;
- kwNoP:
- fProgress := false;
- kwT:
- fTime := true;
- kwNoT:
- fTime := false;
- kwHelp:
- BEGIN
- DoShowUsage;
- fRetCode := RC_Normal;
- Stop('');
- END;
- OTHERWISE
- SyntaxError(Concat(ArgV^[fArgVIndex]^, ' <invalid option>'));
- END;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.DoStartProgress;
-
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := fProgName;
- WriteLn(Diagnostic);
- WriteLn(Diagnostic, aStr, ' (Ver ', Version, ') ');
- WriteLn(Diagnostic);
- WriteLn(Diagnostic);
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
- PROCEDURE TPascalTool.Initialize; OVERRIDE;
- BEGIN
- INHERITED Initialize;
- fArgVIndex := 0;
- fCursorCount := 0; { prepare to spin that cursor}
- fInterrupted := FALSE; { becomes TRUE when interrupted}
- fKeyWordList := NIL; { keywords to this command }
- fProgName := ''; { Program's file name}
- fProgress := FALSE;
- fRetCode := RC_Normal;
- fStartDateTime := 0; { Date/Time at start of tool }
- fStartTicks := 0; { tickcount at start of tool }
- fTime := FALSE;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TInit}
-
- PROCEDURE TPascalTool.IPascalTool;
-
- VAR
- holdIndex: Integer;
- prevSig: SignalHandler;
- arg: Str255;
- theDateTime: Longint;
- anAssociation: TAssociation;
-
- BEGIN
- IObject;
- gPascalTool := SELF;
- fStartTicks := TickCount;
- GetDateTime(theDateTime);
- fStartDateTime := theDateTime;
-
- SpinCursor(1);
- prevSig := IEsignal(SIGINT, @Intr);
-
- fProgName := ArgV^[0]^;
- gProgName := fProgName;
- fRetCode := RC_ParmErrs;
-
- IF fInterrupted THEN
- Stop('');
-
- New(anAssociation);
- anAssociation.IAssociation;
- fKeyWordList := anAssociation;
-
- InstallKeyWords;
- END;
-
- {--------------------------------------------------------------------------------------------------}
- {$S TRes}
-
- PROCEDURE TPascalTool.Run;
-
- VAR
- fi: FailInfo;
-
- LABEL 1000;
-
- PROCEDURE HdlFailure(error: Integer;
- message: Longint);
-
- VAR
- theErr: OSErr;
- theText: Str255;
-
- BEGIN
- theErr := error;
- IF theErr <> noErr THEN
- BEGIN
- GetSysErrText(theErr, @theText);
- WriteLn(Diagnostic, kErrorMarker, gProgName, ': ', theText);
- fRetCode := RC_Abort;
- END;
- GOTO 1000;
- END;
-
- BEGIN
- CatchFailures(fi, HdlFailure);
- fArgVIndex := 1;
- WHILE fArgVIndex < ArgC DO {ArgC is the number of args plus one}
- BEGIN
- fCursorCount := fCursorCount + 1;
- RotateCursor(fCursorCount);
- ProcessArg(ArgV^[fArgVIndex]^);
- fArgVIndex := fArgVIndex + 1;
- END;
- UnloadSeg(@InitUPascalTool);
- fRetCode := RC_Normal;
-
- IF fProgress THEN
- DoStartProgress;
- DoToolAction;
- IF fTime THEN
- WriteLn(Diagnostic, 'Elapsed time: ', (TickCount - fStartTicks) / 60: 1: 2, ' seconds');
- Success(fi);
- 1000:
- IEexit(Ord(fRetCode));
- END;
- {--------------------------------------------------------------------------------------------------}
- {$S TRes}
-
- PROCEDURE TPascalTool.DoToolAction;
-
- VAR
- aStr: Str255;
-
- BEGIN
- aStr := fProgName;
- WriteLn(Diagnostic, kErrorMarker, aStr, ': Forgot to override the default tool action');
- END;
-